#include <Arduino.h>
#include <birdwater.h>



void BirdWaterInit(BirdWaterConfig *config) {
  config->g_WorkState = WORK_STATE_IDLE;
  // config->g_SleepTime = CONFIG_SLEEP_TIME_INIT;
  config->g_dc12v = 12;
  config->g_SSTimer = 0;
  config->g_TimeConfig = 1;
  config->g_waterEN = 0;
}

void BirdWaterTaskEn(BirdWaterConfig *config) {
  config->g_WorkState = WORK_STATE_WORK;  
  config->g_waterEN = 1;
  config->g_SSTimer = 0;
}

void birdwaterTimer100ms()
{
    Serial.println();
    Serial.printf("g_SSTimer:%d,状态：%d bump:%d sleeptime:%d g_WaterTime:%d,wakeupsourse:%d", g_BirdWaterConfig.g_SSTimer, g_BirdWaterConfig.g_WorkState, g_BirdWaterConfig.g_PumpOn, g_BirdWaterConfig.g_SleepTime,g_BirdWaterConfig.g_WaterTime,g_BirdWaterConfig.wakeupsourse);
}

void birdwaterSS(BirdWaterConfig *config) {
  int ircmd = config->g_IrReceiverCmd;
  int dc12v = config->g_dc12v;

  config->g_IrReceiverCmd = 0;

  switch (ircmd)
  {
    case IR_CMD_UP:
    case IR_CMD_LEFT:
    case IR_CMD_RIGHT:
      config->g_WorkState = WORK_STATE_TEST;
      config->g_SSTimer = 0;
      break;
    case IR_CMD_FLAGJIN:
      config->g_WorkState = WORK_STATE_TIMESET;
      config->g_SSTimer = 0;
      break;
    case IR_CMD_FLAGXING:
      config->g_WorkState = WORK_STATE_STUDY;
      config->g_SSTimer = 0;
      break;
    case IR_CMD_DOWN:
      config->g_WorkState = WORK_STATE_WORK;
      config->g_SSTimer = 0;
      break;
    case IR_CMD_OK:
      config->g_WorkState = WORK_STATE_IDLE;
      config->g_SSTimer = 0;
      break;
  }

  switch (config->g_WorkState) {
    case WORK_STATE_IDLE:
      if (((config->g_waterEN == 1) && (dc12v > 10))
          || (ircmd == IR_CMD_DOWN)) {
        config->g_WorkState = WORK_STATE_WORK;
        config->g_SSTimer = 0;
      } else if ((ircmd == IR_CMD_UP)
                 || (ircmd == IR_CMD_LEFT)
                 || (ircmd == IR_CMD_RIGHT)) {
        config->g_WorkState = WORK_STATE_TEST;
        config->g_SSTimer = 0;
      } else if ((ircmd == IR_CMD_FLAGJIN)) {
        config->g_WorkState = WORK_STATE_TIMESET;
        config->g_SSTimer = 0;
      } else if ((ircmd == IR_CMD_FLAGXING)) {
        config->g_WorkState = WORK_STATE_STUDY;
        config->g_SSTimer = 0;
      } else if (config->g_SSTimer > 5 * 10) {  //待机时间 5秒
        config->g_WorkState = WORK_STATE_PREPARE_SLEEP;
        config->g_SSTimer = 0;
      }

      break;
    case WORK_STATE_WORK:
      config->g_waterEN = 0;
      config->workTimePre = millis();
      if ((ircmd == IR_CMD_OK) || (dc12v < 8) || (config->g_SSTimer >= (config->g_WaterTime*10))) {
        config->g_WorkState = WORK_STATE_IDLE;
        config->g_SSTimer = 0;
        
      }

      break;
    case WORK_STATE_TIMESET:
      if ((ircmd == IR_CMD_OK) || (config->g_SSTimer >= 600)) {
        config->g_WorkState = WORK_STATE_IDLE;
        config->g_SSTimer = 0;
      }
      else 
      {
          if (ircmd == IR_CMD_0)  //测试值
          {
            config->g_TimeConfig = 0;
            config->g_WaterTime = 5;
            config->g_SleepTime = 2;
            config->g_SSTimer = 0;
          }
          else if (ircmd == IR_CMD_1)
          {
            config->g_TimeConfig = 1;
            config->g_SleepTime = 24*3600;
            config->g_SSTimer = 0;
          }
          else if (ircmd == IR_CMD_2)
          {
            config->g_TimeConfig = 2;
            config->g_SleepTime = 24*2*3600;
            config->g_SSTimer = 0;
          }
          else if (ircmd == IR_CMD_3)
          {
            config->g_TimeConfig = 3;
            config->g_SleepTime = 24*3*3600;
            config->g_SSTimer = 0;
          }
          else if (ircmd == IR_CMD_4)
          {
            config->g_TimeConfig = 4;
            config->g_SleepTime = 24*4*3600;
            config->g_SSTimer = 0;
          }
          else if (ircmd == IR_CMD_5)
          {
            config->g_TimeConfig = 5;
            config->g_SleepTime = 24*5*3600;
            config->g_SSTimer = 0;
          } else if (ircmd == IR_CMD_6)
          {
            config->g_TimeConfig = 6;
            config->g_SleepTime = 24*6*3600;
            config->g_SSTimer = 0;
          }
          else if (ircmd == IR_CMD_9)  //临时大量需求 每天6分钟
          {
            config->g_TimeConfig = 9;
            config->g_WaterTime = 120;
            config->g_SSTimer = 0;
          }
          else if (ircmd == IR_CMD_8)  //临时中量需求 每天6秒
          {
            config->g_TimeConfig = 8;
            config->g_WaterTime = 30;
            config->g_SSTimer = 0;
          }
          else if (ircmd == IR_CMD_7)  //临时小量需求 每天6秒
          {
            config->g_TimeConfig = 7;
            config->g_WaterTime = 10;
            config->g_SSTimer = 0;
          }
      }
      break;
    case WORK_STATE_TEST:
      if ((ircmd == IR_CMD_OK) || (config->g_SSTimer >= 600)) {
        config->g_WorkState = WORK_STATE_IDLE;
        config->g_SSTimer = 0;
      }
      if ((ircmd == IR_CMD_UP)) {
        config->g_PumpOn = 1;
        config->g_SSTimer = 0;
      } else if (ircmd == IR_CMD_LEFT) {

        config->g_MoveSpeed = -100;
        config->g_SSTimer = 0;

      } else if (ircmd == IR_CMD_RIGHT) {

        config->g_MoveSpeed = 100;
        config->g_SSTimer = 0;

      }else if (config->g_SSTimer > 2) {
        config->g_PumpOn = 0;
        config->g_MoveSpeed = 0;
      }


      break;
    case WORK_STATE_PREPARE_SLEEP:
    case WORK_STATE_SLEEP:
       if (config->g_SSTimer > 2 * 10) {  //等待休眠时间 2秒
        //config->g_WorkState = WORK_STATE_SLEEP;
      }
      break;
    case WORK_STATE_STUDY: //60秒自动退出
      if ((config->g_SSTimer >= 600)) {    //(ircmd == IR_CMD_OK) || 不可退出
        config->g_WorkState = WORK_STATE_IDLE;
        config->g_SSTimer = 0;
      } 
      break;
    default:
      config->g_WorkState = WORK_STATE_IDLE;
      config->g_SSTimer = 0;
      break;
  }
}